//
//  TestMoveViewController.m
//  iOSDeme
//
//  Created by wxf on 2024/8/25.
//

#import "TestMoveViewController.h"

@interface TestMoveViewController ()

@property(nonatomic,weak) IBOutlet UIButton *testHead;
@end

@implementation TestMoveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    UIButton *testHead = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    testHead.backgroundColor = [UIColor blueColor];
//    testHead.imageView = [UIImage imageNamed:@""];
    
    [self.view addSubview:testHead];
    self.testHead = testHead;
    
    
    
    UIButton *top = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 50, 50)];
    top.backgroundColor = [UIColor blueColor];
    [top setTag:1];
    [top setTitle:@"上" forState:UIControlStateNormal];
    [top addTarget:self action:@selector(testMove:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:top];
    
    
    UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(50, 450, 50, 50)];
    left.backgroundColor = [UIColor blueColor];
    [left setTag:2];
    [left setTitle:@"左" forState:UIControlStateNormal];
    [left addTarget:self action:@selector(testMove:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:left];
    
    UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(150, 450, 50, 50)];
    right.backgroundColor = [UIColor blueColor];
    [right setTag:3];
    [right setTitle:@"右" forState:UIControlStateNormal];
    [right addTarget:self action:@selector(testMove:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:right];
    
    
    UIButton *down = [[UIButton alloc] initWithFrame:CGRectMake(100, 500, 50, 50)];
    down.backgroundColor = [UIColor blueColor];
    [down setTag:4];
    [down setTitle:@"下" forState:UIControlStateNormal];
    [down addTarget:self action:@selector(testMove:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:down];
    
    
    UIButton *big = [[UIButton alloc] initWithFrame:CGRectMake(210, 450, 50, 50)];
    big.backgroundColor = [UIColor blueColor];
    [big setTitle:@"放大" forState:UIControlStateNormal];
    [big addTarget:self action:@selector(big) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:big];
    
    
    UIButton *small = [[UIButton alloc] initWithFrame:CGRectMake(270, 450, 50, 50)];
    small.backgroundColor = [UIColor blueColor];
    [small setTitle:@"缩小" forState:UIControlStateNormal];
    [small addTarget:self action:@selector(testSmall) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:small];
}


-(void)testMove:(UIButton *)btn{
    NSLog(@"TestMoveViewController testMove");
    
    NSLog(@"TestMoveViewController btn.tag=%lu", btn.tag);
    
    //动画方法
    [UIView beginAnimations:nil context:nil];
    //设置动画持续时间
    [UIView setAnimationDuration:1.0];
    
    int diff = 50;
    
    if (YES) {
        CGPoint center = self.testHead.center;
        switch (btn.tag) {
            case 1:
                center.y -= diff;
                break;
            case 2:
                center.x -= diff;
                break;
            case 3:
                center.x += diff;
                break;
            case 4:
                center.y += diff;
                break;
                
            default:
                break;
        }
        self.testHead.center = center;
    } else{
        
        CGRect rect = self.testHead.frame;
        switch (btn.tag) {
            case 1:
                rect.origin.y -= diff;
                break;
            case 2:
                rect.origin.x -= diff;
                break;
            case 3:
                rect.origin.x += diff;
                break;
            case 4:
                rect.origin.y += diff;
                break;
                
            default:
                break;
        }
        self.testHead.frame = rect;
    }
    
    //    [self.testHead setFrame:CGRectMake(self.testHead.frame.origin.x,
    //                                          self.testHead.frame.origin.y-10,
    //                                          self.testHead.frame.size.width,
    //                                          self.testHead.frame.size.height)];
    
    [UIView commitAnimations];
}



-(void)big{
    NSLog(@"TestMoveViewController big");
    NSLog(@"TestMoveViewController x=%f", self.testHead.frame.origin.x);
    NSLog(@"TestMoveViewController y=%f", self.testHead.frame.origin.y);
    NSLog(@"TestMoveViewController width %f", self.testHead.bounds.size.width);
    NSLog(@"TestMoveViewController height %f", self.testHead.bounds.size.height);
    
    //    左上角为缩放原点
    //    CGRect frame = self.testHead.frame;
    //    frame.size.width+=10;
    //    frame.size.height+=10;
    //
    //    self.testHead.frame = frame;
    
    //视图中心为缩放原点
    CGRect bounds = self.testHead.bounds;
    bounds.size.width+=10;
    bounds.size.height+=10;
    
    self.testHead.bounds = bounds;
    
    
}

-(void)testSmall{
    NSLog(@"TestMoveViewController testSmall");
    NSLog(@"TestMoveViewController x=%f", self.testHead.frame.origin.x);
    NSLog(@"TestMoveViewController y=%f", self.testHead.frame.origin.y);
    NSLog(@"TestMoveViewController width %f", self.testHead.bounds.size.width);
    NSLog(@"TestMoveViewController height %f", self.testHead.bounds.size.height);
    
    CGRect bounds = self.testHead.bounds;
    bounds.size.width -=10;
    bounds.size.height -=10;
    
    self.testHead.bounds =bounds;
    
}



@end
